package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.core.common.log.LoggerUtils;
import com.jule.teenpatti.base.bean.ItemConfigBean;
import com.jule.teenpatti.base.dao.bean.User;
import com.jule.teenpatti.base.enums.ErrorCodeEnum;
import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.game.dao.DBUtil;
import com.jule.teenpatti.game.model.PlayerInfo;
import com.jule.teenpatti.game.model.eenum.PlayerStateEnum;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_ChangeCroupierAck_50053;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.LogService;
import com.jule.teenpatti.game.service.ProductionService;
import com.jule.teenpatti.game.service.TableService;
import com.jule.teenpatti.game.utils.log.TableLogUtil;
import com.jule.teenpatti.game.vavle.notice.NoticeBroadcastMessages;
import com.jule.teenpatti.log.service.LogReasons;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JoloGame_ChangeCroupierReq_50053 extends ClientReq {
    private long time;
    private JoloGame.JoloGame_ChangeCroupierReq req;

    public JoloGame_ChangeCroupierReq_50053(int functionId) {
        super(functionId);
    }

    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_ChangeCroupierReq.parseFrom(blob);
        this.setTable(TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId()));
    }

    @Override
    public void processImpl() throws Exception {
        log.debug("收到消息-> " + functionId + ", reqNum-> " + header.reqNum + ", req->" + req.toString());
        String userId = req.getUserId();
        String roomId = req.getRoomId();
        String tableId = req.getTableId();
        String itemId = req.getItemId();
        AbstractTable table = getTable();
        PlayerInfo player = table.getPlayer(userId);
        JoloGame.JoloGame_ChangeCroupierAck.Builder ack = JoloGame.JoloGame_ChangeCroupierAck.newBuilder();
        try {
            ack.setUserId(userId);
            ack.setRoomId(roomId);
            ack.setTableId(tableId);
            ack.setItemId(itemId);
            ack.setResult(1);


            if (null == player) {
                ack.setResult(-1).setResultMsg(ErrorCodeEnum.GAME_50050_2.getCode());
                ctx.writeAndFlush(new JoloGame_ChangeCroupierAck_50053(ack.build(), header));
                return;
            }

            if (player.getState().getValue() == PlayerStateEnum.spectator.getValue()) {
                ack.setResult(-2).setResultMsg(ErrorCodeEnum.GAME_50014_1.getCode());
                ctx.writeAndFlush(new JoloGame_ChangeCroupierAck_50053(ack.build(), header));
                return;
            }
            /**单价*/
            ItemConfigBean model = ProductionService.getInstance().getGiftConfig(Integer.parseInt(itemId));
            if (model == null) {
                log.error("-3 can't found croupier config");
                ack.setResult(-3).setResultMsg(ErrorCodeEnum.GAME_50053_2.getCode());
                ctx.writeAndFlush(new JoloGame_ChangeCroupierAck_50053(ack.build(), header));
                return;
            }
            /*int price = (int) (model.getPrice() / 100d * table.getRoomConfig().getAnte() + model.getExtraPrice());
            if (player.getPlayScoreStore() < price) {
                ack.setResult(-4).setResultMsg(ErrorCodeEnum.GAME_50053_1.getCode());
                ctx.writeAndFlush(new JoloGame_ChangeCroupierAck_50053(ack.build(), header));
                return;
            }*/
            int price = 0;

            player.minusPlayScoreStore(price, LogReasons.CommonLogReason.GAME_DEALER);
            ack.setCurrPlayScore(player.getPlayScoreStore());
            ctx.writeAndFlush(new JoloGame_ChangeCroupierAck_50053(ack.build(), header));

            //发送通知
            NoticeBroadcastMessages.moneyChangeNotice(table,userId,player.getPlayScoreStore());

            //发送换荷官记录
            User user = DBUtil.selectByPrimaryKey(userId);
            LogService.OBJ.sendChangeDealerLog(user, itemId, price);
            TableLogUtil.chnageCroupier(functionId, "chnageCroupier", userId, player.getNickName(), table.getPlayType() + "",
                    table.getRoomId(), table.getTableId(), price);
            long timeMillis = System.currentTimeMillis() - time;
            if (timeMillis > GameConst.COST_TIME) {
                LoggerUtils.performance.info("ChangeCroupierReq_50053,cost time:{},req:{}", timeMillis, req.toString());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
        }
    }
}
